﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace imaginecup
{
    /// <summary>
    /// 还需要修改坐标的显示
    /// </summary>
    public partial class ShowMessage : UserControl
    {
        private ServiceReference1.DataProviderSoapClient getMessageClient = new imaginecup.ServiceReference1.DataProviderSoapClient();
        private List<ServiceReference1.CMessage> mylist;
        private bool IsLoaded = false;
        int curindex;
        List<MessageItem> messagecontrollist;
        double east, west, north, south;

        /// <summary>
        /// 构造函数，需要指定范围
        /// </summary>
        /// <param name="e"></param>
        /// <param name="w"></param>
        /// <param name="n"></param>
        /// <param name="s"></param>
        public ShowMessage(double e,double w,double n,double s)
        {
            InitializeComponent();
            east = e;
            west = w;
            north = n;
            south = s;
            this.Loaded += new RoutedEventHandler(ShowMessage_Loaded);
        }

        /// <summary>
        /// 空间被载入后进行事件定义和获取数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void ShowMessage_Loaded(object sender, RoutedEventArgs e)
        {
            //定义事件
            AddEvents();

            //显示留言数据
            IsLoaded = false;
            getMessageClient.getMessageAsync(east, west, north, south);
        }

        /// <summary>
        /// 把所有添加事件放到此处
        /// </summary>
        private void AddEvents()
        {
            getMessageClient.getMessageCompleted += new EventHandler<imaginecup.ServiceReference1.getMessageCompletedEventArgs>(getMessageClient_getMessageCompleted);
            btnNext.Click += new RoutedEventHandler(btnNext_Click);
            btnBefore.Click += new RoutedEventHandler(btnBefore_Click);
        }
        /// <summary>
        /// 从服务器端获取数据后进行初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void getMessageClient_getMessageCompleted(object sender, imaginecup.ServiceReference1.getMessageCompletedEventArgs e)
        {
            //显示相关的message
            mylist = new List<ServiceReference1.CMessage>(e.Result);
            messagecontrollist = new List<MessageItem>();

            for (int i = 0; i < mylist.Count; i++)
            {
                messagecontrollist.Add(new MessageItem(mylist[i]));
            }

            curindex = mylist.Count - 1;
            getfocus(curindex);
            IsLoaded = true;
        }

        /// <summary>
        /// 前一条留言
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btnBefore_Click(object sender, RoutedEventArgs e)
        {
            if (curindex != 0)
            {
                curindex--;
                transerfocus(curindex);
            }
        }

        /// <summary>
        /// 下一条留言
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btnNext_Click(object sender, RoutedEventArgs e)
        {
            if (curindex != mylist.Count - 1)
            {
                curindex++;
                transerfocus(curindex);
            }
        }

        /// <summary>
        /// 第i条数据获得焦点
        /// </summary>
        /// <param name="index"></param>
        private void getfocus(int index)
        {
            spMessageItems.Children.Clear();
            //tmpdir.Clear();

            //添加顺序不同，不行
            for (int i = 0; i < mylist.Count; i++)
            {
                int messageindex = (index + 1 + i) % mylist.Count;
                //int tmpindex = mylist.Count- (i + mylist.Count - index) % mylist.Count;

                //ServiceReference1.CMessageItem tmpi = mylist[messageindex];
                //MessageItem mi = new MessageItem(tmpi);

                //这里怎样添加属性事件？
                //messagecontrollist[messageindex].SetValue(Name, "a");
                messagecontrollist[messageindex].SetValue(Canvas.TopProperty, 20.0d + 100 * i);
                messagecontrollist[messageindex].SetValue(Canvas.LeftProperty, 40.0d);
                messagecontrollist[messageindex].pProjection.RotationX = -60 + i * 10;
                messagecontrollist[messageindex].MouseLeftButtonUp += new MouseButtonEventHandler(mi_MouseLeftButtonUp);
                //tmpdir[mi.GetHashCode()] = messageindex;

                spMessageItems.Children.Add(messagecontrollist[messageindex]);
            }

            //进一步改进使用doubleanimation

        }

        /// <summary>
        /// 为数据的变换添加动画效果，还没有实现，暂时用getfocus
        /// </summary>
        /// <param name="index"></param>
        private void transerfocus(int index)
        {
            //建立转移，或者从原来的focus到现在的foucus的转移

            for (int i = 0; i < messagecontrollist.Count; i++)
            {
                int messageindex = (index + 1 + i) % messagecontrollist.Count;

            }


            //添加storyboardboard

            //触发事件

            //暂时用getfocus
            getfocus(index);
        }

        /// <summary>
        /// 鼠标点击释放的效果
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void mi_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            //throw new NotImplementedException();

            //get the index
            int i;
            for (i = 0; i < messagecontrollist.Count; i++)
            {
                if (messagecontrollist[i].GetHashCode() == sender.GetHashCode())
                    break;
            }
            if (i == messagecontrollist.Count)
            {
                throw new Exception("hash code error");
            }

            curindex = i;
            transerfocus(i);
        }


    }
}
